package com.example.utils;

import com.alibaba.fastjson.JSON;
import com.example.constants.Constant;
import com.example.enums.RequestEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.ModelMap;

/**
 * @author guoQ.z
 * @date 2021/1/13 15:27
 * TODO
 */
@Slf4j
public class SignUtil {

    /**
     * 加密消息 - 公钥加密
     *
     * @param data 加密的内容
     * @return 返回加密后的对象
     */
    public static ModelMap getSignData(Object data) {
        ModelMap modelMap = new ModelMap();
        modelMap.put(RequestEnum.version.name(), Constant.VERSION);
        modelMap.put(RequestEnum.timestamp.name(), System.currentTimeMillis());

        String body = "";
        try {
            body = RSAUtils.encryptByPublicKey(JSON.toJSONString(data), Constant.PUBLIC_KEY);
        } catch (Exception e) {
            log.error("签名错误, ex: {}", e.getStackTrace());
        }
        modelMap.put(RequestEnum.body.name(), body);
        return modelMap;
    }

    /**
     * 解密
     *
     * @param data
     * @return
     */
    public static String decryptData(Object data) {
        String body = "";
        try {
            body = RSAUtils.decryptByPublicKey("", Constant.PUBLIC_KEY);
        } catch (Exception ex) {
            log.error("解签错误, ex: {}", ex.getStackTrace());
        }
        return body;
    }
}
